//
//  TTDiamondPopShareView.m
//  TaoTuo
//
//  Created by admin on 2019/12/17.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTDiamondPopShareView.h"
#import <WXApi.h>

@interface TTDiamondPopShareView ()

@property (weak, nonatomic) IBOutlet UIButton *zsBtn;
@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (weak, nonatomic) IBOutlet UILabel *subLabel;
@property (weak, nonatomic) IBOutlet UILabel *beilvLabel;
@property (weak, nonatomic) IBOutlet UILabel *addLabel;
@property (weak, nonatomic) IBOutlet UILabel *lirunLabel;
///不带证书号的文字
@property (nonatomic, copy) NSString *originalText;
@property (nonatomic, copy) NSString *beforeContent;

@property (nonatomic, assign) CGFloat count;

@property (nonatomic, strong) NSTimer *timer;

@end


@implementation TTDiamondPopShareView

- (void)awakeFromNib
{
    [super awakeFromNib];
    
    self.layer.cornerRadius = 10;
    self.width = SCREEN_WIDTH - 60;
    
    self.textView.layer.cornerRadius = 5;
    self.subLabel.layer.cornerRadius = 5;
    self.subLabel.layer.masksToBounds = YES;
    self.subLabel.userInteractionEnabled = YES;
    
    self.beilvLabel.layer.cornerRadius = 5;
    self.beilvLabel.layer.masksToBounds = YES;
    
    self.addLabel.layer.cornerRadius = 5;
    self.addLabel.layer.masksToBounds = YES;
    self.addLabel.userInteractionEnabled = YES;
    
    
    
    self.count = 1;
    
}


- (NSString *)getAfterPartContentWithPrice:(CGFloat)price
{
    return [NSString stringWithFormat:@"%.0f元 折扣%.2f%%", price, [self calculateDiscountWithPrice:price]];
}

///计算折扣
- (CGFloat)calculateDiscountWithPrice:(CGFloat)price
{
    //汇率
    CGFloat dollarRate = [TTLoginSingle sharedSingle].dollarRate;
    CGFloat r1 = self.model.Rap.floatValue * self.model.DiaSize.floatValue * dollarRate;
    CGFloat result = price / r1 * 100 - 100;
    
    return result;
}

- (void)setModel:(TTDiamondModel *)model
{
    _model = model;
    
    self.beforeContent = [NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@", model.Shape, model.DiaSize, model.Color, model.Clarity, model.Cut, model.Sym, model.Polish, model.Flour, model.Milky, model.Browness, model.EyeClean];
    
    NSString *afterContent = [self getAfterPartContentWithPrice:self.model.Price.floatValue];
    
    self.originalText = [NSString stringWithFormat:@"%@ %@", self.beforeContent, afterContent];
    
    self.textView.text = self.originalText;
    
    self.zsBtn.hidden = model.SouthAfrica;
    
}

///加减的时候重新设置值
- (void)resetTextViewText
{
    CGFloat price = self.model.Price.floatValue * self.count;
    
    NSString *afterContent = [self getAfterPartContentWithPrice:price];
    
    NSString *zsStr = self.zsBtn.selected ? [NSString stringWithFormat:@"%@:%@", self.model.Cert, self.model.CertNo] : @"";
    
    self.originalText = [NSString stringWithFormat:@"%@ %@", self.beforeContent, afterContent];
    self.textView.text = [NSString stringWithFormat:@"%@ %@ %@", self.beforeContent, afterContent, zsStr];
    
    self.lirunLabel.text = [NSString stringWithFormat:@"利润:%.0f", price - self.model.Price.floatValue];
}

///减号按下
- (IBAction)subButtonTouchDown:(UIButton *)sender {
    
    YYWeakProxy *proxy = [YYWeakProxy proxyWithTarget:self];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:proxy selector:@selector(subAction) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

///减号按下在里面抬起
- (IBAction)SubButtonTouchUpInside:(UIButton *)sender {
    
    [self subAction];
    
    [self.timer invalidate];
    self.timer = nil;
    
}

///减号按下在外面抬起
- (IBAction)SubButtonTouchUpOutside:(UIButton *)sender {
    
    [self.timer invalidate];
    self.timer = nil;
}


///加号按下
- (IBAction)addButtonTouchDown:(UIButton *)sender {
    
    YYWeakProxy *proxy = [YYWeakProxy proxyWithTarget:self];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:proxy selector:@selector(addAction) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    
}

///加号按下在里面抬起
- (IBAction)addButtonTouchUpInside:(UIButton *)sender {
    
    [self addAction];
    [self.timer invalidate];
    self.timer = nil;
}

///加号按下在外面抬起
- (IBAction)addButtonTouchUpIOutside:(id)sender {
    
    [self.timer invalidate];
    self.timer = nil;
    
}


- (void)subAction
{
    
    if (self.count <= 1) {
        self.count = 1;
        return;
    }
    
    self.count-=0.01;
    self.beilvLabel.text = [NSString stringWithFormat:@"%.2f", self.count];
    
    [self resetTextViewText];

}


- (void)addAction
{
    
    self.count+=0.01;
    self.beilvLabel.text = [NSString stringWithFormat:@"%.2f", self.count];
    
    [self resetTextViewText];

}

- (IBAction)zsBtnClick:(UIButton *)sender {
    
    sender.selected = !sender.selected;
    
    if (sender.selected) {
        self.textView.text = [NSString stringWithFormat:@"%@ %@:%@", self.originalText, self.model.Cert, self.model.CertNo];
    } else {
        self.textView.text = self.originalText;
    }
}


// 复制
- (IBAction)fuzhiBtnClick:(id)sender {
    
    UIPasteboard *paste = [UIPasteboard generalPasteboard];
    paste.string = self.textView.text;
    [[UIApplication sharedApplication].keyWindow makeToast:@"内容已复制到剪贴板"];
    
    [self hideInWindow];
}


// 分享
- (IBAction)shareBtnClick:(id)sender {
    
    if (![WXApi isWXAppInstalled]) {
        [[UIApplication sharedApplication].keyWindow makeToast:@"未安装微信"];
        return;
    }
    SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
    req.bText = YES;
    req.text = self.textView.text;
    req.scene = WXSceneSession;
    [WXApi sendReq:req completion:^(BOOL success) {
        [[UIApplication sharedApplication].keyWindow makeToast:@"分享成功"];
    }];
    
    [self hideInWindow];
}

// 关闭
- (IBAction)closeBtnClick:(id)sender {
    
    [self hideInWindow];
    
}

@end
